package adv410;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SaxXMLhander extends DefaultHandler {

    @Override//读到了文本节点
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String s = new String(ch, start, length);
        System.out.println("结点值:" + s);
    }

    @Override//文档结束
    public void endDocument() throws SAXException {
        System.out.println("文档结果了");
        super.endDocument();
    }

    @Override//节点结束了
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        System.out.println(localName + "," + qName + "元素结束了");
//
//		if(qName.equals("book"))
//		{
//			//list.add(b);
//		}
    }

    @Override//文档开始
    public void startDocument() throws SAXException {
        System.out.println("文档开始了");
        super.startDocument();
    }

    //Book b=null;
    @Override//元素开始
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        System.out.println("元素开始了");
        System.out.println(localName + "," + qName);
        for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
            System.out.println(attributes.getQName(i) + "," + attributes.getValue(i));

        }
    }


    //测试类
    public static void main(String[] args) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse("file:///E:\\java_workspace\\IdeaProjects\\java83\\java高级\\src\\adv47\\java83.xml", new SaxXMLhander());


    }
}